<?php
/**
 * Component: upload
 *	Author: TuyenND
 *	Date: 10-09-2007
 */
class UploadComponent extends Object {

	var $ext_array = array();
	var $max_file_size;
	var $file_size;
	var $upload_dir;
	var $temp_file_name;
	var $file_name;
	var $error;
	var $new_file_name;

	/**
	 * Upload file
	 */
	function doUpload(){
		/**
		 * Kiem tra loai file cho phep upload
		 */
		if($this->validateFileExtension()){
			/**
			 * Kiem tra dung luong file cho phep upload
			 */
			if($this->validateFileSize()){
				/**
				 * Kiem tra co ton tai file tam de chuan bi cho viec upload hay ko
				 */
				if(is_uploaded_file($this->temp_file_name)){
					/**
					 * Thuc hien lenh upload file
					 */
					$this->createNewFileName();
					if(move_uploaded_file($this->temp_file_name, $this->upload_dir.$this->new_file_name)){
						$this->setErrorMessage("Upload success");
						return true;
					}
				}else{
					return false;
				}
			}else{
				$this->setErrorMessage("Size not more than: ". $this->max_file_size);
				return false;
			}
		}else{
			$this->setErrorMessage("You cannot upload this file type");
			return false;
		}
	}
	/**
	 * Kiem tra loai file
	 */
	function validateFileExtension(){
		$extension = strtolower($this->getFileExtension());
		if(in_array($extension, $this->ext_array)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Kiem tra dung luong file
	 */
	function validateFileSize() {
		$max_file_size = trim($this->max_file_size);
		$size = $this->file_size;
		if($size > $max_file_size) {
			return false;
		}else {
			return true;
		}
	}
	/**
	 * Lay phan mo rong cua file
	 * @file_name: Ten file
	 */
	function getFileExtension() {
		$file_name = $this->file_name;
		$i = strrpos($this->file_name, ".");
		if(!$i) {
			return "";
		}
		$l = strlen($file_name) - $i;
		$ext = substr($file_name, $i+1, $l);
		return $ext;
	}
	/**
	 * Dung luong file toi da cho phep upload
	 * @size: Dung luong file tinh bang bytes
	 */
	function setMaxFileSize($size){
		$this->max_file_size = $size;
	}
	/**
	 * Dung luong file
	 * @size: Dung luong file
	 */
	function setFileSize($size){
		$this->file_size = $size;
	}
	/**
	 * Thu muc chua file upload
	 *	@str_folder: Duong dan vat ly toi thu muc chua file upload
	 */
	function setUploadDir($str_folder){
		$this->upload_dir = $str_folder;
	}
	/**
	 * Thong tin file tam
	 *	@temp_file_name: File tam
	 */
	function setTempFileName($temp_file_name){
		$this->temp_file_name = $temp_file_name;
	}
	/**
	 * Thiet lap ten file
	 *	@file_name: Ten file
	 */
	function setFileName($file_name){
		$this->file_name = $file_name;
	}
	/**
	 * Tao ten file moi	*
	 */
	function createNewFileName(){
		$t = substr(time(), -7);
		$file_name = str_replace('-','_', $this->file_name);
		$file_name = str_replace(' ','_', $file_name);
		$this->new_file_name = $t.'_'.$file_name;
	}
	/**
	 * Lay ten file moi
	 */
	function getNewFileName(){
		return $this->new_file_name;
	}
	/**
	 * Thong bao loi
	 *	@message: Noi dung cau thong bao
	 */
	function setErrorMessage($message){
		$this->error = $message;
	}
	/**
	 * Lay thong bao loi
	 */
	function getErrorMessage(){
		return $this->error;
	}
	/**
	 * Dinh nghia mang cac file ko dc phep upload
	 */
	function setAllowFileType($array_file_type){
		$this->ext_array = $array_file_type;
	}
	/**
	 * Lay mang cac file ko dc phep upload
	 */
	function getAllowFileType(){
		return $this->ext_array;
	}
}
?>